<template>
	<view class="conter">
		<header-box :title="title"></header-box>

		<view class="conter-text">
			<view class="bgc">
				<view class="box-btn">
					<view class="" style="margin-bottom: 30rpx;">
						{{nameInit}}名称
					</view>
					<view class="">
						<input type="text" v-model="name" placeholder="请输入单位名称" placeholder-style="font-size: 28rpx" />
					</view>
				</view>
				<view class="box-btn">
					<view class="" style="margin-bottom: 30rpx;">
						联系人名
					</view>
					<view class="">
						<input type="text" v-model="contact" placeholder="请填写备注" placeholder-style="font-size: 28rpx" />
					</view>
				</view>

				<view class="box-btn">
					<view class="" style="margin-bottom: 30rpx;">
						联系电话
					</view>
					<view class="">
						<input type="number" maxlength="11" v-model="phone" placeholder="请输入联系电话"
							placeholder-style="font-size: 28rpx" />
					</view>
				</view>

				<view class="box-btn1">
					<view class="" style="margin: 20rpx 0 30rpx;">
						选择分类
					</view>
					<view class="box-btn2">
						<u-radio-group v-model="value" @change="radioGroupChange">
							<u-radio active-color="#6268E4" v-for="(item, index) in list" :key="index"
								:name="item.name">
								{{item.name}}
							</u-radio>
						</u-radio-group>
					</view>
				</view>
			</view>
		</view>

		<view class="add bg-gradual-green" @click="addName">
			保存
		</view>
		
		<!-- 信息提示-->
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import headerBox from '../../components/header/header.vue'

	export default {
		components: {
			headerBox
		},
		data() {
			return {
				title: '供应商',
				name: '',
				contact: '',
				phone: '',
				list: [{
						name: '供应商',
						disabled: true,
						index: 1,
					},
					{
						name: '客户',
						disabled: false,
						index: 2,
					},
				],
				// u-radio-group的v-model绑定的值如果设置为某个radio的name，就会被默认选中
				value: '供应商',
				nameInit: '',
			}
		},
		onLoad() {
			console.log(this.value);
			this.nameInit = this.value
		},
		methods: {
			// 选中某个单选框时，由radio时触发
			radioGroupChange(e) {
				this.nameInit = e

			},


			async addName() {

				// 添加供应商
				if (this.nameInit == '供应商') {
					if (this.name == '' || this.phone == '' || this.contact == '') return this.$refs.uToast.show({ title: '内容不能未空', type: 'error' });

					const res = await this.$u.post("api/Invoic/supplier_add?supplier_name=" + this.name + "&phone=" +
						this.phone + "&person_name=" + this.contact);

					if (res.code !== 1) {
						
						this.$refs.uToast.show({ title: res.msg, type: 'error' });
						
					} else {
						
						this.$refs.uToast.show({ title: res.msg, type: 'success' });
						this.name = ''
						this.phone = ''
						setTimeout( function() {
							uni.navigateBack({
								delta: 1
							})
						},2000)
					}
					
				} else {
					// 客户
					if (this.name == '' || this.phone == '' || this.contact == '') return this.$refs.uToast.show({ title: '内容不能未空', type: 'error' });
					
					const res = await this.$u.post("api/Invoic/wldw_add?name=" + this.name + "&phone=" +
						this.phone + "&person_name=" + this.contact);
					
					if (res.code !== 1) {
						this.$refs.uToast.show({ title: res.msg, type: 'error' });
					} else {
						this.$refs.uToast.show({ title: res.msg, type: 'success' });
						this.name = ''
						this.phone = ''
						this.contact = ''
						uni.navigateBack({
							delta: 1
						})
					}
					
				}

			}
		}
	}
</script>

<style lang="scss" scoped>
	.conter {
		width: 100%;
		height: 100%;
		position: relative;
	}

	.conter-text {
		padding: 30rpx;

		.bgc {
			width: 100%;
			height: 610rpx;
			padding: 0 30rpx;
			border-radius: 10rpx;
			background-color: #FFFFFF;

			.box-btn {
				width: 100%;
				height: 144rpx;
				font-size: 30rpx;
				padding: 20rpx 0;
				border-bottom: 1rpx solid #F0F0F0;
			}

			.box-btn1 {
				width: 100%;
				height: 144rpx;
				font-size: 30rpx;
			}
		}
	}

	.add {
		position: fixed;
		bottom: 30rpx;
		left: 50%;
		width: 90%;
		height: 90rpx;
		line-height: 90rpx;
		border-radius: 45rpx;
		transform: translateX(-50%);
		text-align: center;
		color: #FFFFFF;
		font-size: 32rpx;
	}
</style>
